package com.morder.application;

import com.morder.util.FileUtils;

import android.content.Context;
import android.content.SharedPreferences;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.provider.Settings.Secure;

public class AppPreference {

  private static final String PREFER_NAME = "MOrderAppPreference";
  private static Context mContext;
  private static SharedPreferences mPrefer;
  private static final String FIRST_BOOT_KEY = "first_boot";
  private static final boolean FIRST_BOOT_DEFAULT_VALUE = false;
  private static final String LOGIN_SUCCESS_KEY = "login_success";
  private static final boolean LOGIN_SUCCESS_DEFAULT_VALUE = false;
  
  private static final String MAC_KEY = "mac";
  private static final String MAC_DEFAULT_VALUE = "";
  
  private static final String ANDROID_KEY = "androidId";
  private static final String ANDROID_DEFAULT_VALUE = "";

  public static void initialize(Context context) {
    AppPreference.mContext = context;
    mPrefer = context.getSharedPreferences(PREFER_NAME, Context.MODE_WORLD_WRITEABLE);
    initFolder();
    SharedPreferences.Editor editor = mPrefer.edit();
    initMacAddress(editor);
    initAndroidId(editor);
    editor.commit();
  }

  private static void initFolder() {
    FileUtils.createPath(BuildConfig.DREADER_DATA_FILE_PATH);
    FileUtils.createPath(BuildConfig.DREADER_DATA_DOWN_PATH);
    FileUtils.createPath(BuildConfig.DREADER_DATA_ICON_PATH);
  }

  private static void initMacAddress(SharedPreferences.Editor editor) {
    WifiManager wifi = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = wifi.getConnectionInfo();
    String mac = info.getMacAddress();
    editor.putString(MAC_KEY, mac);
  }
  
  private static void initAndroidId(SharedPreferences.Editor editor) {
    String androidId = Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID);
    editor.putString(ANDROID_KEY, androidId);
  }
  
  public static Context getContext() {
    return mContext;
  }
  
  private static void putBoolean(String key, boolean value) {
    SharedPreferences.Editor editor = mPrefer.edit();
    editor.putBoolean(key, value);
    editor.commit();
  }
  
  private static boolean getBoolean(String key, boolean defValue) {
    return mPrefer.getBoolean(key, defValue);
  }
  
  private static String getString(String key, String defValue){
    return mPrefer.getString(key, defValue);
  }

  public static void markFirstBootFinished(boolean isFirst) {
    putBoolean(FIRST_BOOT_KEY, isFirst);
  }

  public static final boolean isFirstBootFinished() {
    return getBoolean(FIRST_BOOT_KEY, FIRST_BOOT_DEFAULT_VALUE);
  }
  
  public static void markLoginSuccess(boolean isFirst) {
    putBoolean(LOGIN_SUCCESS_KEY, isFirst);
  }

  public static final boolean isLoginSuccess() {
    return getBoolean(LOGIN_SUCCESS_KEY, LOGIN_SUCCESS_DEFAULT_VALUE);
  }
  
  public static String getMacAddress(){
    return getString(MAC_KEY, MAC_DEFAULT_VALUE);
  }

  public static String getAndroidId(){
    return getString(ANDROID_KEY, ANDROID_DEFAULT_VALUE);
  }
  
  public static void terminate() {
    mContext = null;
    mPrefer = null;
  }
}
